﻿package com.game.view.layer.gameMap
{
	import AssetClass.Trans1Inter;
	
	import com.game.event.GameEvent;
	
	import config.Config;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class NewWorldMap extends GameMapBase implements IGameMap
	{
		
		private const MAP_INDEX:Object = {MIN: 20, MAX: 22};
		
		public function NewWorldMap():void
		{
			super();
			this.addEventListener(Event.ADDED_TO_STAGE, this.added, false, 0, true);
			this.addEventListener(Event.REMOVED_FROM_STAGE, this.removed, false, 0, true);
			this.init();
		}
		
		public var s20_1:MovieClip;
		public var s20_2:MovieClip;
		public var s20_3:MovieClip;
		public var s21_1:MovieClip;
		public var s21_2:MovieClip;
		public var s21_3:MovieClip;
		public var s22_1:MovieClip;
		public var s22_2:MovieClip;
		public var s22_3:MovieClip;
		public var s23_1:MovieClip;
		public var s23_2:MovieClip;
		public var s23_3:MovieClip;
		public var hdbtn:MovieClip;
		
		private function init():void
		{
			for (var stageNum:int = MAP_INDEX.MIN; stageNum <= MAP_INDEX.MAX; stageNum++)
			{
				levelNum = 0;
				for (var levelNum:int = 1; levelNum <= 3; levelNum++)
				{
					if (this["s" + stageNum + "_" + levelNum] != null)
					{
						this["s" + stageNum + "_" + levelNum].gotoAndStop(1);
						this["s" + stageNum + "_" + levelNum].buttonMode = true;
					}
				}
			}
			this.hdbtn.buttonMode = true;
		}
		
		private function added(param1:Event):void
		{
			var bStage:int = Config.getIns().userData.maxStage;
			var bLevel:int;
			if (bStage > MAP_INDEX.MAX)
			{
				bStage = MAP_INDEX.MAX;
			}
			for (var i:int = MAP_INDEX.MIN; i <= bStage; i++)
			{
				var j:int;
				if (i < bStage)
				{
					bLevel = 3;
				}
				else
				{
					bLevel = Config.getIns().userData.maxLevel;
				}
				for (j = 1; j <= bLevel; j++)
				{
					var str1:String = "s" + i + "_" + j;
					if (this[str1] != null)
					{
						this[str1].addEventListener(MouseEvent.CLICK, this.onSelected);
						this[str1].addEventListener(MouseEvent.ROLL_OVER, this.mOver);
						this[str1].addEventListener(MouseEvent.ROLL_OUT, this.mOut);
					}
				}
			}
			var str2:String = "s" + Config.getIns().userData.maxStage + "_" + Config.getIns().userData.maxLevel;
			if (this.hasOwnProperty(str2) && this[str2] != null)
			{
				this[str2].gotoAndStop(2);
			}
			this.hdbtn.addEventListener(MouseEvent.CLICK, this.newWorldClick);
			this.hdbtn.addEventListener(MouseEvent.ROLL_OVER, this.mOver);
			this.hdbtn.addEventListener(MouseEvent.ROLL_OUT, this.mOut);
			var menu:MapMenu = this.getChildByName("showedMapMenu") as MapMenu;
			if (menu != null)
			{
				this.removeChild(menu);
			}
			menu = new MapMenu();
			menu.x = menu.y = 0;
			menu.name = "showedMapMenu";
			this.addChild(menu);
		}
		
		private function removed(param1:Event):void
		{
			for (var i:int = MAP_INDEX.MIN; i <= MAP_INDEX.MAX; i++)
			{
				for (var j:int = 0; j <= 3; j++)
				{
					var str1:String = "s" + i + "_" + j;
					if (this.hasOwnProperty(str1) && this[str1] != null)
					{
						this[str1].removeEventListener(MouseEvent.CLICK, this.onSelected);
						this[str1].removeEventListener(MouseEvent.ROLL_OVER, this.mOver);
						this[str1].removeEventListener(MouseEvent.ROLL_OUT, this.mOut);
					}
				}
			}
			this.hdbtn.removeEventListener(MouseEvent.CLICK, this.newWorldClick);
			this.hdbtn.removeEventListener(MouseEvent.ROLL_OVER, this.mOver);
			this.hdbtn.removeEventListener(MouseEvent.ROLL_OUT, this.mOut);
		}
		
		private function mOver(param1:MouseEvent):void
		{
			param1.currentTarget.gotoAndStop(3);
		}
		
		private function onSelected(param1:MouseEvent):void
		{
			var str1:String = param1.currentTarget.name.replace("s", "");
			Config.getIns().curStage = int(str1.split("_")[0]);
			Config.getIns().curLevel = int(str1.split("_")[1]);
		}
		
		private function mOut(param1:MouseEvent):void
		{
			var str1:String = param1.currentTarget.name;
			if (!this.hasOwnProperty(str1) || this[str1] == null)
			{
				return;
			}
			if (str1 == "s" + Config.getIns().userData.maxStage + "_" + Config.getIns().userData.maxLevel)
			{
				this[str1].gotoAndStop(2);
			}
			else
			{
				this[str1].gotoAndStop(1);
			}
		}
		
		private function newWorldClick(param1:MouseEvent):void
		{
			new Trans1Inter();
		}
	}
}
